import smtplib
from email.mime.text import MIMEText
from email.header import Header
from ReadConfigFile import ReadConfigFile

class SendingEmails:
    def __init__(self,form_addr, to_addr, smtp_server, smtp_port, password):

        self.form_addr = form_addr
        self.to_addr = to_addr
        self.smtp_server = smtp_server
        self.smtp_port = smtp_port
        self.password = password

    def send_html_email(self, subject, message):
        #创建MIMEText对象实例，设置内容为html，并设置编码格式为utf-8
        msg = MIMEText(message, 'html', 'utf-8')
        #设置邮件主题
        msg['Subject'] = Header(subject)
        #设置发件人
        msg['From'] = self.form_addr
        #设置收件人
        msg['To'] = self.to_addr
        try:
            #创建SMTP对象实例，连接到SMTP服务器
            smtp_server = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port)
            #登录邮箱
            smtp_server.login(self.form_addr, self.password)
            #发送邮件
            smtp_server.sendmail(self.form_addr, self.to_addr, msg.as_string())
            #关闭SMTP服务器连接
            smtp_server.quit()
            print('邮件发送成功')
        except smtplib.SMTPException as e:
            print('邮件发送失败', e)